﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnPhatTrienPhanMem.Models;

namespace DoAnPhatTrienPhanMem.Controllers
{
    public class VienKhoaHocController : BaseController
    {
        //
        // GET: /VienKhoaHoc/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Details()
        {
            var ID = Request["ID"];
            ViewData["ID"] = ID;
            ViewData["TenVien"] = helper.GetOne<Vien>(s => s.ID.ToString() == ID).TenVien;
            return View();
        }

        public ActionResult AddOrEdit()
        {
            ViewData["ID"] = "";
            var ID = Request["ID"];
            if (!String.IsNullOrEmpty(ID)) ViewData["ID"] = ID;
            return View();
        }

        public ActionResult DanhMucCanBo()
        {
            var ID = Request["ID"];
            ViewData["ID"] = ID;
            return View();
        }

        public ActionResult ThemCanBo()
        {
            var ID = Request["ID"];
            ViewData["ID"] = ID;
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Save(FormCollection values)
        {
            var result = new Dictionary<String, Object>();

            try
            {
                var ID = values["ID"];
                var entity = String.IsNullOrEmpty(ID)
                             ? new Vien()
                             : helper.GetOne<Vien>(s => s.ID.ToString() == ID);
                if (entity == null)
                {
                    return WarningMessage("Không tồn tại");
                }
                // Load data to entity
                entity = (Vien)GetDataFromForm(entity, values);
                if (entity.ID == Guid.Empty)
                {
                    #region Insert
                    if (helper.EntityExists<Vien>(s => s.TenVien == entity.TenVien))
                    {
                        return WarningMessage("Tên viện đã tồn tại");
                    }
                    entity.ID = Guid.NewGuid();
                    helper.Insert(entity);
                    #endregion
                }
                else
                {
                    #region Update
                    if (helper.EntityExists<Vien>(s => s.TenVien == entity.TenVien && s.ID != entity.ID))
                    {
                        return WarningMessage("Tên viện đã tồn tại");
                    }
                    helper.Update(entity, s => s.ID.ToString() == ID);
                    #endregion
                }
                result.Add("success", true);
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return WarningMessage("Lỗi :" + e.Message);
            }
        }

        public JsonResult Delete(Guid ID)
        {
            IDictionary<String, Object> result = new Dictionary<String, Object>();
            try
            {
                var SanPham = helper.GetOne<Vien>(s => s.ID == ID);
                if (SanPham == null)
                {
                    return WarningMessage("Viện không tồn tại");
                }
                helper.Delete<Vien>(s => s.ID == ID);
                result.Add("success", true);
            }
            catch (Exception e)
            {
                return WarningMessage("Lỗi :" + e.Message);
            }
            return Json(result);
        }

        public JsonResult GetAll()
        {
            var result = new Dictionary<String, Object>();
            var resultList = helper.GetTable<VienOverView>().OrderBy(s => s.TenVien).ToList();
            var tochuc = helper.GetTable<ToChucCanBo>().ToList();
            foreach (var canbo in tochuc)
            {
                if (canbo.ThuocBanVien == "VIEN")
                {
                    canbo.MaPhongBan = helper.GetOne<Phong>(s => s.MaPhong == canbo.MaPhongBan).MaVien;
                }
            }
            foreach (var vien in resultList)
            {
                vien.SLcanbo = tochuc.Count(s => s.MaPhongBan == vien.MaVien && s.ThuocBanVien == "VIEN");
            }
            result.Add("data", resultList);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetByID()
        {
            var ID = Request["ID"];
            var result = new Dictionary<String, Object>();
            var resultList = helper.GetTable<Vien>().Where(s => s.ID.ToString() == ID).ToList();
            result.Add("data", resultList);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

    }
}
